/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.AttributeType;
import Model.Clusterization;
import Model.DataSet;
import javax.swing.JOptionPane;

/**
 *
 * @author Snowangelic
 */
public class StarClusterController {
    DataSet dataSet;

    public StarClusterController(){
        dataSet=null;
    }

    public void setClusterName(Clusterization currentClusterization, int clusterIndex, String name) {
        dataSet.setClusterName(currentClusterization, clusterIndex, name);
    }

    public void setModel(DataSet dataSet){
        this.dataSet=dataSet;
    }

    public void addAttribute(String name,AttributeType type){
        Boolean validName=true;
        for(int i=0;i<dataSet.getAttributeNbr();i++)
            if(dataSet.getAttributeName(i).equals(name)){
                System.out.println("dataSet.getAttributeName("+i+")= "+dataSet.getAttributeName(i)+"; name= "+name);
                validName=false;
            }
        if(validName) dataSet.addAttribute(name, type);
        else{
            JOptionPane.showMessageDialog(null,
                "An attribute with this name already exists.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
        }
    }

    public void addAction(){
        if(dataSet.getActionNbr()<10000){
            dataSet.addAction();
        }
        else{
           JOptionPane.showMessageDialog(null,
                "The maximum action number has been reached.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
        }
    }

    public void setValue(int actionIndex,int attributeIndex,String value){
        if(actionIndex<dataSet.getActionNbr()&&
           attributeIndex<dataSet.getAttributeNbr()){

            if(value.toString().equals("")||value==null) dataSet.removeValue(dataSet.getAction(actionIndex), dataSet.getAttribute(attributeIndex));
            else if(dataSet.getAttributeType(attributeIndex)==AttributeType.Numerical){
                try{
                    dataSet.setValue(dataSet.getAction(actionIndex), dataSet.getAttribute(attributeIndex),Float.valueOf(value));
                }
                catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null,
                    "Numerical value expected.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                }
            }
            else{
                dataSet.setValue(dataSet.getAction(actionIndex), dataSet.getAttribute(attributeIndex),value);
            }
        }
    }

}
